import requests
from lxml import etree
l1 = ['https://cs.lianjia.com/ershoufang/']
l2 = [f'https://cs.lianjia.com/ershoufang/pg{i}/' for i in range(2, 6)]
l3 = l1 + l2
# 一定不要把打开和关闭文件写到循环里面
f = open('data.txt', 'w', encoding='utf-8')
for url in l3:  # 基础的课程10 公共操作 enumerate有讲到
    head_data = {
        # 字典，需要有一个键值对
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
    }
    html = requests.get(url, headers=head_data)

    html = etree.HTML(html.text)
    title = html.xpath('//div[@class="info clear"]/div[1]/a/text()')


    flood1 = html.xpath('//div[@class="info clear"]/div[2]/div/a[1]/text()')
    flood2 = html.xpath('//div[@class="info clear"]/div[2]/div/a[2]/text()')
    flood = zip(flood1, flood2)

    price = html.xpath('//div[@class="info clear"]/div[6]/div[1]/span/text()')

    price1 = html.xpath('//div[@class="info clear"]/div[6]/div[2]/span/text()')


    for title, flood, price, price1 in zip(title, flood, price, price1):
        f.write(' | '.join([title, '- '.join(flood).replace(' ', ''), price + '万', price1]) + '\n')

f.close()
